package com.musifind.wsclient.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.musifind.wsclient.helpers.UserHelper;
import com.musifind.wsclient.objects.User;
import com.musifind.wsclient.url.UserRestURIConstants;

public class UserService extends BaseService {

	public static User createUser(User user) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
		User response = restTemplate.postForObject(SERVER_URI + UserRestURIConstants.CREATE_USER, user, User.class);
		return response;
	}

	public static User getUser(int userId) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
		User response = restTemplate.getForObject(SERVER_URI + UserRestURIConstants.GET_USER + userId, User.class);
		return response;
	}

	public static List<User> getAllUser() {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
		List<LinkedHashMap> emps = restTemplate
				.getForObject(SERVER_URI + UserRestURIConstants.GET_ALL_USER, List.class);
		List<User> users = new ArrayList<User>();
		for (LinkedHashMap map : emps) {
			// Kullanici kullanici = new Kullanici();
			// kullanici.setId(new Long("" + map.get("id")));
			// kullanici.setAd((String) map.get("ad"));
			// kullanici.setSoyad((String) map.get("soyad"));
			// kullanici.setMail((String) map.get("mail"));
			// kullanici.setSifre((String) map.get("sifre"));
			// kullanici.setDogumTarih(new Date((Long) map.get("dogumTarih")));
			// kullanici.setCinsiyet((String) map.get("cinsiyet"));
			// kullanici.setDeleted((Boolean) map.get("deleted"));
			// kullanici.setGcmRegId((String) map.get("gcmRegId"));
			// kullanici.setFaceId((String) map.get("faceId"));
			// kullanici.setFaceUser((Boolean) map.get("faceUser"));
			// kullanici.setImage((String) map.get("image"));
			// kullanici.setTarih(new Date((Long) map.get("tarih")));
			// kullanicis.add( kullanici);
			users.add(UserHelper.getUserFromLinkedHashMap(map));
			// private Date tarih;
			// private Set<Eslesme> eslesmesFrom = new HashSet<Eslesme>(0);
			// private Set<Eslesme> eslesmesTo = new HashSet<Eslesme>(0);
			// private Set<Dinleme> dinlemes = new HashSet<Dinleme>(0);
			// System.out.println("ID=" + map.get("id") + ",Name=" + map.get("name") + ",CreatedDate="
			// + map.get("createdDate"));
		}
		return users;
	}

	public static User deleteUser(int id) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
		User user = restTemplate.getForObject(SERVER_URI + UserRestURIConstants.DELETE_USER + id, User.class);
		return user;
	}

	public static User getFaceUser(String faceId) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
		User response = restTemplate.getForObject(SERVER_URI + UserRestURIConstants.FACE_USER_EXIST + faceId,
				User.class);
		return response;
	}

	public static User getUser(String mail) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
		User response = restTemplate.getForObject(SERVER_URI + UserRestURIConstants.USER_EXIST + mail, User.class);
		return response;
	}

	public User setPublicUser(String id) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
		User response = restTemplate.getForObject(SERVER_URI + UserRestURIConstants.SET_PUBLIC_USER + id, User.class);
		return response;
	}

	public User setPrivateUser(String id) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
		User response = restTemplate.getForObject(SERVER_URI + UserRestURIConstants.SET_PRIVATE_USER + id, User.class);
		return response;
	}

}
